package com.sec.android.app.myfiles.widget.listview;

import android.util.SparseArray;

import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.navigation.NavigationChangedObserver.NavigationChangedListener;
import com.sec.android.app.myfiles.navigation.NavigationInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yunju.cha on 2016-10-14.
 */
public class SelectionManager implements NavigationChangedListener {
    private static SparseArray<SelectionManager> sSelectionMap = new SparseArray<>();
    private ArrayList<Integer> mSelectedPositions; // Selected child positions.
    private AbsMyFilesFragment.ActionModeType mActionModeType; // Action mode type before popup view

    private int mSelectedGroupPosition; // Selected group position in search.
    private ArrayList<FileRecord> mSelectedChildRecords; // Selected child positions in search.
    private List<Integer> mExpandedGroupPositions; // Expanded group positions in search.

    private SelectionManager() {
        mSelectedPositions = new ArrayList<>();
        mActionModeType = AbsMyFilesFragment.ActionModeType.NORMAL;
        mSelectedChildRecords = new ArrayList<>();
        mExpandedGroupPositions = new ArrayList<>();
    }

    public static SelectionManager getInstance(int processId) {
        SelectionManager ret = sSelectionMap.get(processId);
        if (ret == null) {
            ret = new SelectionManager();
            sSelectionMap.put(processId, ret);
        }
        return ret;
    }

    public void clearInstance(int processId) {
        SelectionManager selectionMgr = sSelectionMap.get(processId);
        if (selectionMgr != null) {
            selectionMgr.clear();
            selectionMgr.mSelectedPositions = null;
            sSelectionMap.remove(processId);
        }
    }

    public void clear() {
        if (mSelectedPositions != null) {
            mSelectedPositions.clear();
        }
        mActionModeType = AbsMyFilesFragment.ActionModeType.NORMAL;
        mSelectedGroupPosition = -1;
        clearSelectedRecords();
    }

    @Override
    public void onNavigationChanged(NavigationInfo preInfo, NavigationInfo curInfo) {
        clear();
    }

    public void savePositions(ArrayList<Integer> selectedPosition) {
        if (mSelectedPositions == null) {
            mSelectedPositions = new ArrayList<>();
        }
        mSelectedPositions = selectedPosition;
    }

    public ArrayList<Integer> getSelectedPositions() {
        return mSelectedPositions;
    }

    public void saveActionModeType(AbsMyFilesFragment.ActionModeType actionModeType) {
        mActionModeType = actionModeType;
    }

    public AbsMyFilesFragment.ActionModeType getActionModeType() {
        return mActionModeType;
    }

    public void saveGroupPosition(int groupPosition) {
        mSelectedGroupPosition = groupPosition;
    }

    public int getSelectedGroupPosition() {
        return mSelectedGroupPosition;
    }

    public void saveSelectedRecords(ArrayList<FileRecord> selectedRecord) {
        if (mSelectedChildRecords == null) {
            mSelectedChildRecords = new ArrayList<>();
        }
        mSelectedChildRecords = selectedRecord;
    }

    public ArrayList<FileRecord> getSelectedChildRecords() {
        return mSelectedChildRecords;
    }

    public void clearSelectedRecords() {
        if (mSelectedChildRecords != null) {
            mSelectedChildRecords.clear();
        }
    }

    public void saveExpandedGroupPositions(List<Integer> expandedGroupPosition) {
        if (mExpandedGroupPositions == null) {
            mExpandedGroupPositions = new ArrayList<>();
        }
        mExpandedGroupPositions = expandedGroupPosition;
    }

    public List<Integer> getExpandedGroupPositions() {
        return mExpandedGroupPositions;
    }
}
